﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Expression.Interactivity;
using System.Windows.Interactivity;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using System.Windows.Threading;

namespace girapong
{
    public class enemy
    {
        public bar _bar { get; set; }
        public int v { get; set; }
        public int reachedEdge { get; set; }

        public enemy(System.Windows.Media.Color c, int speed)
        {
            _bar = new bar(c, 100, 0);
            _bar.move(100, 0);
            this.v = speed;
            this.reachedEdge = 1;
        }

        public void moveAxisXbyAmount(double amount)
        {
            System.Windows.Shapes.Rectangle rE = _bar.rectangle;
            double total = rE.Margin.Left + amount;
            double final = total + rE.Width;
            Boolean b = final <= screen.HEIGHT;
            Boolean m = (rE.Margin.Left + amount) < 0;
            if ((final >= 0) && b && !m)
            {
                _bar.move(total, rE.Margin.Top);
            }
            else
            {
                _bar.turnBarPerpendicularly(bar.STAND);
                double resultantPosX = 0;
                double c = rE.Margin.Top;
                if (c != 0) c = screen.WIDTH - rE.Height;
                if (!b)
                {
                    resultantPosX = screen.HEIGHT - rE.Width;
                }

                if (resultantPosX == 0)
                {
                    reachedEdge = 4;
                }
                else
                {
                    reachedEdge = 2;
                }
                if ((c != 0) && (amount < 0)) c -= amount;
                _bar.move(resultantPosX, c);

            }
        }

        public void moveAxisYbyAmount(double amount)
        {
            System.Windows.Shapes.Rectangle rE = _bar.rectangle;
            double total = rE.Margin.Top + amount;
            double final = total + rE.Height;
            Boolean b = final <= screen.WIDTH;
            Boolean m = (rE.Margin.Top + amount) < 0;
            if ((final >= 0) && b && !m)
            {
                _bar.move(rE.Margin.Left, total);
            }
            else
            {
                _bar.turnBarPerpendicularly(bar.LYING);
                double resultantPosY = 0;
                double c = rE.Margin.Left;
                if (c != 0) c = screen.HEIGHT - rE.Width;
                if (!b)
                {
                    resultantPosY = screen.WIDTH - rE.Height;
                }

                if (resultantPosY == 0) reachedEdge = 1;
                else reachedEdge = 3;
                if ((c != 0) && (amount < 0)) c -= amount;
                _bar.move(c, resultantPosY);

            }
        }

        public void moveAxisXbyBall(ball _ball, int displacement)
        {
            System.Windows.Shapes.Rectangle rE = _bar.rectangle;
            System.Windows.Shapes.Ellipse eP = _ball.circle;
            double halfBar = (rE.Width / 2);
            if ((rE.Margin.Left + halfBar) <= eP.Margin.Left)
            {
                moveAxisXbyAmount(displacement);
            }
            else if ((rE.Margin.Left + halfBar) > (eP.Margin.Left + eP.Width))
            {
                moveAxisXbyAmount(-displacement);
            }
        }
        public void moveAxisYbyBall(ball _pelota, int desplazamiento)
        {
            System.Windows.Shapes.Rectangle rE = _bar.rectangle;
            System.Windows.Shapes.Ellipse eP = _pelota.circle;
            double halfBar = (rE.Height / 2);
            if ((rE.Margin.Top + halfBar) <= eP.Margin.Top)
            {
                moveAxisYbyAmount(desplazamiento);
            }
            else if ((rE.Margin.Top + halfBar) > (eP.Margin.Top + (eP.Width / 2)))
            {
                moveAxisYbyAmount(-desplazamiento);
            }
        }
    }
}
